package com.atguigu.spzx.manager.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.service.OrderInfoService;
import com.atguigu.spzx.model.dto.order.OrderStatisticsDto;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import com.atguigu.spzx.model.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;


    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {

        //查询统计结果数据，返回list集合
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto);

        //日期列表
        List<String> dateList = new ArrayList<>();
        for (OrderStatistics orderStatistics : orderStatisticsList) {
            String formatDate = DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd");
            dateList.add(formatDate);
        }
        //统计金额列表
        List<BigDecimal> amountList = new ArrayList<>();
        for (OrderStatistics orderStatistics : orderStatisticsList) {
            BigDecimal totalAmount = orderStatistics.getTotalAmount();
            amountList.add(totalAmount);
        }

        //创建OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);
        return orderStatisticsVo;
    }
}
